package com.ruoyi.system.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CommonConstant;
import com.ruoyi.common.core.domain.entity.admin.AdminCropDB;
import com.ruoyi.common.core.domain.entity.admin.AdminPlotCropRelDB;
import com.ruoyi.common.exception.global.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.admin.AdminCropVarietiesMapper;
import com.ruoyi.system.mapper.admin.AdminPlotCropMapper;
import com.ruoyi.system.mapper.admin.AdminPlotMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AdminPlotCropRelService extends ServiceImpl<AdminPlotCropMapper, AdminPlotCropRelDB> {
    @Resource
    private AdminPlotMapper adminPlotMapper;

    @Resource
    private AdminCropVarietiesMapper adminCropMapper;


    // 增加关系
    public void addRel(AdminPlotCropRelDB adminPlotCropRelDB) {
        // 确认两个id皆有效
        Long plotId = adminPlotCropRelDB.getPlotId();
        List<Long> cropIds = adminPlotCropRelDB.getCropIds();

        LambdaQueryWrapper<AdminCropDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AdminCropDB::getId, cropIds);
        List<AdminCropDB> adminCropDBS = adminCropMapper.selectList(queryWrapper);
        if (!adminPlotMapper.checkIsActive(plotId)) {
            throw new CustomException("请检查菜地有效性");
        }
        if (ObjectUtils.isEmpty(adminCropDBS) || !Objects.equals(cropIds.size(), adminCropDBS.size())) {
            throw new CustomException("请检查作物有效性");
        }

        if (ObjectUtils.isEmpty(adminPlotCropRelDB.getPlantingTime())) {
            adminPlotCropRelDB.setPlantingTime(DateUtils.getNowDate());
        }
        ArrayList<AdminPlotCropRelDB> needSaveList = new ArrayList<>();
        for (AdminCropDB item : adminCropDBS) {
            AdminPlotCropRelDB relDB = new AdminPlotCropRelDB();
            relDB.setPlotId(plotId);
            relDB.setCropId(item.getId());

            // 预计收获时间=当前时间+收获天数
            relDB.setEstimatedHarvestTime(DateUtils.addDays(adminPlotCropRelDB.getPlantingTime(), item.getDaysToHarvest().intValue()));
            relDB.setMultiHarvest(item.getMultiHarvest());
            relDB.setHarvestIntervalDays(item.getHarvestIntervalDays());
            relDB.setPlantingTime(adminPlotCropRelDB.getPlantingTime());
            needSaveList.add(relDB);
        }
        // 批量保存
        this.saveBatch(needSaveList);
    }

    public List<AdminPlotCropRelDB> findCropListByPlotId(Long plotId) {
        return this.list(new LambdaQueryWrapper<>(AdminPlotCropRelDB.class).eq(AdminPlotCropRelDB::getPlotId, plotId).eq(AdminPlotCropRelDB::getIsActive, CommonConstant.CONSTANT_NUM.ONE));
    }
}
